package com.zpark.it.controller;

import com.zpark.it.param.AddressListParam;
import com.zpark.it.param.AddressParam;
import com.zpark.it.param.AddressRemoveParam;
import com.zpark.it.pojo.Address;
import com.zpark.it.service.AddressService;
import com.zpark.it.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cglib.transform.impl.AddPropertyTransformer;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;



/**
 * @Title: AddressController 地址的控制器
 * @Author Charles
 * @Package com.zpark.it.controller
 * @Date 2023/7/20 16:24
 */
@RestController
@RequestMapping("/user/address")
public class AddressController {

    @Autowired
    private AddressService addressService;

    @PostMapping("/list")
    public R list(@RequestBody @Validated AddressListParam addressListParam, BindingResult result){
        if (result.hasErrors()){
            return R.fail("参数异常，查询失败");
        }

        return addressService.list(addressListParam.getUserId());
    }
    @PostMapping("/save")
    public R save(@RequestBody @Validated AddressParam addressParam, BindingResult result){
        if (result.hasErrors()){
            return R.fail("保存失败");
        }
        Address address=addressParam.getAdd();
        address.setUserId(addressParam.getUserId());
        return addressService.save(address);
    }
    @DeleteMapping("/remove")
    public R remove(@RequestBody @Validated AddressRemoveParam addressRemoveParam,BindingResult result){
        if (result.hasErrors()){
            return R.fail("插入异常，删除失败");
        }
        return addressService.remove(addressRemoveParam.getId());
    }
}
